﻿using System.Windows.Controls;
using System.Windows.Input;

namespace MyHome.Configuration.Views
{
    /// <summary>
    /// Interaction logic for PluginConnectionView.xaml
    /// </summary>
    public partial class PluginConnectionView : UserControl
    {
        private bool mouseDown;

        public PluginConnectionView()
        {
            InitializeComponent();
        }

        private void Line_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            mouseDown = true;
        }

        private void Line_MouseUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (mouseDown)
            {
                mouseDown = false;
                // clicked
                var connection = (MyHome.Configuration.ViewModel.PluginConnectionViewModel)DataContext;
                connection.IsSelected = true;
            }
        }

        private void Line_MouseLeave(object sender, MouseEventArgs e)
        {
            e.Handled = true;
            mouseDown = false;
        }

        private void Line_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                var connection = (MyHome.Configuration.ViewModel.PluginConnectionViewModel)DataContext;
                if (connection.IsSelected)
                    connection.DeleteCommand.Execute(null);
            }
        }
    }
}